﻿
//{{{ file documentation -------------------------------------------------------

/*!
 * @file     tools.cpp
 * @brief    dicodor tools class implementation
 */

//}}} --------------------------------------------------------------------------
//{{{ includes / predeclarations -----------------------------------------------

// @(#) $Id: tools.cpp 50 2012-05-21 12:47:32Z nsouchon-dico@altern.org $

#include "tools.h"

#include <QtCore/QFile>

//}}} --------------------------------------------------------------------------
//{{{ bool Tools::fileCopyByBlocks (const QString & srcName, const QString & dstName, const int bufferSize) -

// ns: restored

bool Tools::fileCopyByBlocks (const QString& srcName, const QString& dstName, const int bufferSize)
{
    QFile src (srcName);
    if (!src.exists ()) return false; // source à copier n'existe pas

    QFile dst (dstName);
    if (dst.exists ()) return false; // destination existe déjà

    if (!src.open (QIODevice::ReadOnly)) return false;  // peux pas ouvrir source
    if (!dst.open (QIODevice::Append)) return false;  // peux pas ouvrir destination

    int bytesReadTotal = 0;

    while (!src.atEnd ())
    {
        QByteArray buffer (src.read (bufferSize));
        int bytesInBuffer = buffer.size ();
        bytesReadTotal += bytesInBuffer;

        dst.write (buffer);
        if (src.error () || dst.error ()) return false; // il y a une erreur, vérifier
    }

    return true;
}

//}}} --------------------------------------------------------------------------

